package org.chengn.level_easy.context2788;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title: Context2788
 * @Author ChengN
 * @Package org.chengn.level_easy.context2788
 * @Date 2024/1/20 17:10
 * @description: 按分隔符拆分字符串
 */
public class Context2788 {
    public List<String> splitWordsBySeparator(List<String> words, char separator) {
        List<String> strs = new ArrayList<>();
        for (String word : words) {
            strs.addAll(split(word, separator));
        }

        return strs;
    }

    private List<String> split(String word, char separator) {
        List<String> splitRes = new ArrayList<>();

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < word.length(); ++i) {
            if (word.charAt(i) == separator) {
                if (sb.length() > 0)
                    splitRes.add(sb.toString());

                sb.setLength(0);
                continue;
            }

            sb.append(word.charAt(i));
        }
        if (sb.length() > 0) {
            splitRes.add(sb.toString());
        }

        return splitRes;
    }
}
